package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UserUpdateDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.dto.UserInsertDTO;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
//@Controller
@RestController
@RequestMapping("/v1/users")
@Api(tags = "01.用户管理模块")
@Validated
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("insert")
    //@ResponseBody
    @ApiOperation("用户添加")
    public String insert(@RequestBody @Validated UserInsertDTO userInsertDTO) {
        User user = new User();

        BeanUtils.copyProperties(userInsertDTO, user);

        user.setCreated(new Date());
        userMapper.insertUser(user);
        return "用户添加成功";
    }

    @GetMapping("userList")
    //@ResponseBody
    @ApiOperation("查询用户列表")
    public List<UserListVO> userList() {
        log.debug("HelloWorld");
        return userMapper.selectUserList();
    }

    @GetMapping("delect")
    //@ResponseBody
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int")
    public String delect(@Range(min = 1, max = 100, message = "用户id必须在1~100之间") @RequestParam Integer id) {
        userMapper.deleteUser(id);
        return "用户删除成功";
    }

    @PostMapping("update")
    //@ResponseBody
    @ApiOperation("修改用户信息")
    public String update(UserUpdateDTO userUpdateDTO){
        userMapper.updateUser(userUpdateDTO);
        return "用户修改成功";
    }
}
